
const jwt = require("jsonwebtoken")
const guard=(req, res,next)=>{
    // 判断用户是否在登录页面；
    // 判断用户的登录状态；
    // 如果是登录请求放行
    // 如果不是，重定向安排登录页面
    // var timestamp = Date.parse(new Date());
    // var date = new Date(req.session.cookie._expires);
    // var date=new Date(timestamp)
    // console.log(req.url);
    if(req.url !='/login' && ( req.session.token==null||!req.session.token)){
        res.redirect('/admin/login');
    }else{
        // console.log(req.session.token);
        if(req.url == '/users' ){
            if(jwt.decode(req.session.token).username =='wlgzs'){
                next()
            }else{
                res.redirect('/admin/index');
            }
            console.log(jwt.decode(req.session.token)); 
        }else{
            next();
        }
        // console.log(req.url);
       
        // console.log(timestamp);
        // console.log(date<req.session.cookie._expires);
        // console.log(timestamp>date);
    }
}
module.exports=guard;